package practiceTest.chap04.work2;

import java.util.concurrent.Delayed;

/**
 * Created by root on 2017/7/14.
 */
public class Deque {
    private long[] queArray;
    private int maxSize;
    private int nItems;
    private int left;
    private int right;

    public Deque(int s){
        maxSize = s;
        queArray = new long[maxSize];
        nItems = 0;
        left = -1;
        right = 0;
    }

    public void insertLeft(long item){
        if (left==-1) {
            left = maxSize - 1;
        }
        queArray[left] = item;
        left--;
        nItems++;
    }

    public void insertRight(long item){
        if (right==maxSize) {
            right = 0;
        }
        queArray[right] = item;
        right++;
        nItems++;
    }

    public long removeLeft(){
        left++;
        if (left == maxSize) {
            left = 0;
        }
        nItems--;
        return queArray[left];
    }

    public long removeRight(){
        right--;
        if (right == -1) {
            right = maxSize - 1;
        }
        nItems--;
        return queArray[right];
    }

    public boolean isEmpty() {
        return (nItems==0);
    }

    public boolean isFull() {
        return (nItems==maxSize);
    }

    public void displayDeque(){
        int j = left;
        for (int i=0; i<nItems ; i++) {
            if (j == maxSize-1) {
                j = -1;
                i--;
                continue;
            }
            System.out.print(queArray[++j] + " ");
        }
        System.out.println();

    }




}
